﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DQYT.HTDZ.BLL;
using System.Data;

namespace DQYT.HTDZ.Web.Views.SystemManage
{
    public partial class DeptManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                BDMenuBLL menuBLL = new BDMenuBLL();
                lblPath.Text = menuBLL.GetTitlePath(Request["mnu_id"]);
                if (!IsPostBack)
                {
                    initData("");
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }
        }
        private void initData(string strWhere)
        {
            DataTable dt = new DataTable();
            BDDepartmentBLL dBuMen = new BDDepartmentBLL();
            dt = dBuMen.GetList(strWhere).Tables[0];
            if (dt == null || dt.Rows.Count < 1)
            {
                dt.Rows.Add(dt.NewRow());
            }

            gvData.DataSource = dt;
            gvData.DataKeyNames = new string[] { "BUM_ID" };
            gvData.Columns[0].Visible = false;
            gvData.Columns[1].Visible = false;
            gvData.Columns[5].Visible = false;
            gvData.DataBind();
        }
        protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果没有数据则添加一行“没有数据”行
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string sRow = e.Row.Cells[2].Text.Trim().ToString().Replace("&nbsp;", "");
                    if (string.IsNullOrEmpty(sRow))
                    {
                        return;
                    }
                    string ProNum = DataBinder.Eval(e.Row.DataItem, "BUM_RS").ToString();

                    if (Convert.ToInt32(ProNum) > 0)
                    {
                        (e.Row.Cells[9].FindControl("ckFlag") as CheckBox).Enabled = false;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message.ToString());
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            Response.Redirect("Dept.aspx?action=add");

        }
        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                int n = 0;
                if (this.gvData.Rows.Count > 0)
                {
                    BDDepartmentBLL dBuMen = new BDDepartmentBLL();
                    for (int i = 0; i < gvData.Rows.Count; i++)
                    {
                        CheckBox ck = gvData.Rows[i].Cells[9].FindControl("ckFlag") as CheckBox;
                        if (ck.Checked == true && ck.Enabled == true)
                        {
                            n++;
                            string strID = gvData.DataKeys[i]["BUM_ID"].ToString();
                            int iBUM_ID = Convert.ToInt32(strID);
                            dBuMen.Delete(iBUM_ID);
                        }

                    }
                    if (n == 0)
                    {
                        ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                    }
                    else
                    {
                        initData(" BUM_MC like '%" + txtName.Text.Trim() + "%'");
                    }
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            #region

            try
            {
                if (!string.IsNullOrEmpty(txtName.Text))
                {
                    initData(" BUM_MC like '%" + txtName.Text + "%'");
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }

            #endregion
        }
        protected void lnkbtnEdit_Click(object sender, EventArgs e)
        {
            #region
            string id = (sender as LinkButton).CommandArgument;
            Response.Redirect("Dept.aspx?id=" + id + "&action=update");
            #endregion
        }
    }
}